package com.nabisoft.jaxrs.provider;
 
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.DeserializationConfig;
 
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;

/**
 * Jackson JSON processor could be controlled via providing a custom Jackson ObjectMapper instance. 
 * This could be handy if you need to redefine the default Jackson behavior and to fine-tune how 
 * your JSON data structures look like (copied from Jersey web site). * 
 * @see https://jersey.java.net/documentation/latest/media.html#d0e4799
 */

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Singleton
public class MyJacksonJsonProvider implements ContextResolver<ObjectMapper> {
 
    private static final Logger LOGGER = Logger.getLogger(MyJacksonJsonProvider.class);
    private static final ObjectMapper MAPPER = new ObjectMapper();
     
    static {
      MAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);
      MAPPER.disable(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS);
      //MAPPER.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    }
 
    public MyJacksonJsonProvider() {
        LOGGER.debug("Instantiate MyJacksonJsonProvider");
    }
     
    @Override
    public ObjectMapper getContext(Class<?> type) {
        LOGGER.debug("MyJacksonProvider.getContext() called with type: "+type);
        return MAPPER;
    } 
}